package com.aquila.datacontrol;

import android.app.Activity;
import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{
	private Button button ;
	public static String DATABASE_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + 
			"/.datab";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (SystemConfig.testamentShortStrings == null) {
			SystemConfig.testamentShortStrings= getResources().getStringArray(R.array.jiuyue_jianchen) ;
        }
        SystemConfig.en_testamentNameStrings = getResources().getStringArray(R.array.en_book_name) ;
        SystemConfig.en_testamentShortStrings = getResources().getStringArray(R.array.bible_en_shortname) ;
        SystemConfig.belongStrings = getResources().getStringArray(R.array.search_bound) ;
        
        button = (Button) findViewById(R.id.button) ;
        
        button.setOnClickListener(this) ;
        //检查数据库文件是否存在，不在则拷贝
        DatabaseTools tools=new DatabaseTools(getApplicationContext());
        tools.checkDatabase();
        tools.checkMyDatabase() ;
        
    }
    
    
    private ProgressDialog progressDialog ;
    private void saveToData(){
    	final SaveDataToSqlite dataToSqlite = new SaveDataToSqlite(this) ;
//    	dataToSqlite.readTitle() ;
//    	dataToSqlite.testAsset() ;
    	progressDialog = new ProgressDialog(this) ;
    	progressDialog.setTitle("正在存储...") ;
    	progressDialog.setCancelable(false) ;
    	progressDialog.show() ;
    	new Thread(){
    		public void run() {
    			try {
    			dataToSqlite.methodIn() ;
    			handler.sendEmptyMessage(1) ;
                }
                catch (Exception e) {
                	playMusic() ;
                	handler.sendEmptyMessage(2) ;
                }
    		};
    	}.start() ;
    	
    	
    }
    
    Handler handler = new Handler(){
    	public void handleMessage(android.os.Message msg) {
    		if (msg.what == 1) {
    		progressDialog.cancel();
    		MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.success) ;
        	mediaPlayer.start() ;
            }
    		else if (msg.what == 2) {
			
    		progressDialog.cancel();
			}
    	};
    } ;
    
    private void playMusic() {
    	MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.ling) ;
    	mediaPlayer.start() ;
    }
    
    @Override
    public void onClick(View v) {
    	if (v == button) {
//			SaveDataToSqlite dataToSqlite = new SaveDataToSqlite(this) ;
//			dataToSqlite.testControl() ;
	    	saveToData() ;
		}
    }
    

}
